package com.manage.demo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.manage.demo.annotation.Verify;
import com.manage.demo.service.UserService;
import com.manage.demo.util.JsonUtil;
import java.io.IOException;


@RestController
public class LoginValidController {
	@Autowired
	private UserService userService;
	
	private static final Logger logger = LogManager.getLogger(LoginValidController.class);
	@Verify(sessionVerify=true)
	@RequestMapping(value="/log_valid", method=RequestMethod.POST)
	public Map<String, Object> loginValid(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "user_input", required = true) String user_input,
			@RequestParam(value = "pw_input", required = true) String pw_input,
			@RequestParam(value = "perm", required = true) String perm
			) {
		Map<String, Object> res = new HashMap<String, Object>(); 
	    String regex = "^[0-9]+$";
	    if(user_input.matches(regex)) {  // 正则
	    	if(user_input.length() > 50) {
    			res.put("user_long", "True");
    			
    		}
	        if(pw_input.equals("")) { // 密码未输入
	            res.put("pw_null", "True");
	        }else{
	        	res.put("data", userService.getUserByCondition(user_input, pw_input));
	        	if(pw_input.length() > 50){
	    			res.put("pw_long", "True");
	    			
	    		}else if(res.get("data") == null) {  // 密码输入有误
	                res.put("pw_error", "True");
	            }else if(res.get("data") != null){  // 登录成功
	            	String user = JsonUtil.toJSONString(userService.getUserByCondition(user_input, pw_input));
	            	Map result = JsonUtil.toMap(user);  
	                System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");  
	                for (Object obj : result.keySet()){  
	                	if(result.get("permission").equals("0")) {  // 学生
	                		res.put("stu_name", userService.getStudentByCondition(user_input).getName());
	                		res.put("stu_grade", userService.getStudentByCondition(user_input).getGrade());
	                		res.put("stu_class", userService.getStudentByCondition(user_input).getClass_num());
	                		request.getSession().setAttribute("name", res.get("stu_name"));
	                		request.getSession().setAttribute("grade", res.get("stu_grade"));
	                		request.getSession().setAttribute("class_num", res.get("stu_class"));
	                	}else {  // 老师
	                		System.out.println(userService.getTeacherByCondition(user_input));
	                		res.put("tch_name", userService.getTeacherByCondition(user_input).get("name"));
	                		res.put("tch_course", userService.getTeacherByCondition(user_input).get("course_name"));
	                		request.getSession().setAttribute("name", res.get("tch_name"));
	                		request.getSession().setAttribute("tch_course", res.get("tch_course"));
	                	}
	                	request.getSession().setAttribute("user_input", user_input);
	                	request.getSession().setAttribute("perm", perm);
	                	request.getSession().setAttribute("admin", result.get("administrator"));
	                    System.out.println("key为："+obj+"值为："+result.get(obj));  
	                }  
	            	
	            	res.put("pw_error", "False");
	            }
	        }
	    }else {
	        if(user_input.equals("")) {  // 账号未输入
	            res.put("user_null", "True");
	            
	        }else {  // 账号输入了错了
	            res.put("user_error", "True");
	            if(user_input.length() > 50) {
	    			res.put("user_long", "True");
	    		}
	        }
	    }
	    return res;
    
	}
	
}
	
